package pacman;

import java.awt.Color;
import java.util.ArrayList;
import java.util.Random;

/**
 * This class implements a blue ghost, blind, who moves totally randomly. He never turns back, except if he can't do anything else.
 */
public class cBlueGhost implements iGhost {
    private Position pos;
    private Position lastPos;
    private cCore parent;
    private Random rand = new Random();
    private Color color = new Color(50, 50, 255);
    
    /**
     * Creates a new instance of cBlueGhost.
     * @param   parent  The core of parent application.
     * @param   pos     The position of the new ghost.
     */
    public cBlueGhost(cCore parent, Position pos) {
        this.parent = parent;
        this.pos = pos;
        this.lastPos = pos;
    }
    
    /**
     * Makes the blue ghost to move.
     */
    public void Move() {
        ArrayList<Position> nextPos = new ArrayList<Position>();
        Position tmp = this.pos.clone();
        
        //Tests all positions around current one to see if it's usable
        tmp.incX(1);
        if(parent.isAccessible(tmp,this) && !tmp.equals(lastPos)) {
            nextPos.add(tmp.clone());
        }
        tmp.incX(-2);
        if(parent.isAccessible(tmp,this) && !tmp.equals(lastPos)) {
            nextPos.add(tmp.clone());
        }
        tmp.incX(1);
        tmp.incY(1);
        if(parent.isAccessible(tmp,this) && !tmp.equals(lastPos)) {
            nextPos.add(tmp.clone());
        }
        tmp.incY(-2);
        if(parent.isAccessible(tmp,this) && !tmp.equals(lastPos)) {
            nextPos.add(tmp.clone());
        }
        
        if(nextPos.size()<1) {
            nextPos.add(lastPos);
        }
        
        lastPos = pos;
        pos = nextPos.get(rand.nextInt(nextPos.size()));
    }
    
    /**
     * Returns the current position of the ghost.
     * @return The current position of the ghost.
     */
    public Position getPosition() {
        return pos;
    }
    
    /**
     * Returns the name of the image to load for this kind of ghosts.
     * @return A string containing the name of the image to load for this kind of ghosts.
     */
    public String getImageName() {
        return "blueghost";
    }
}
